<template>
  <div class="white-box">
    <div class="table-mb">
      <el-button type="primary" @click="handleAdd()" :icon="CirclePlus">添加</el-button>
    </div>
    <div class="task-one">
      <div class="task-one__item">
        <span class="task-one__label">优惠卷名称：</span>
        <el-input placeholder="请输入" v-model="searchParams.couponName" @keyup.enter.native="handleSearch" clearable></el-input>
      </div>
      <div class="task-one__item">
        <span class="task-one__label">优惠卷描述：</span>
        <el-input placeholder="请输入" v-model="searchParams.description" @keyup.enter.native="handleSearch" clearable></el-input>
      </div>
      <div class="task-one__btn">
        <el-button type="primary" :loading="searchBtnLoading" :icon="Search"
                   @click="handleSearch">搜索</el-button>
      </div>
    </div>

    <el-table class="table-mb" v-loading="loading" :data="list" :header-cell-style="{ background: '#F5F7FA' }"
              element-loading-background="rgba(255, 255, 255, 0.6)" row-key="sid">
      <el-table-column prop="couponName" label="优惠券名称"></el-table-column>
      <el-table-column prop="amountOff" label="折扣/面值">
        <template #default="{ row }">
          <div>满<el-text type="danger">{{row.minSpend}}</el-text>减<el-text type="danger">{{row.amountOff}}</el-text></div>
        </template>
      </el-table-column>
      <el-table-column prop="description" label="描述" :show-overflow-tooltip="true"></el-table-column>
      <el-table-column align="left" prop="startDate" label="有效期" min-width="100">
        <template #default="{ row }">
          <div>开始：{{row.startDate}}</div>
          <div>结束：{{row.endDate}}</div>
        </template>
      </el-table-column>
<!--      <el-table-column prop="note" label="剩余"></el-table-column>-->
<!--      <el-table-column prop="note" label="已领取"></el-table-column>-->
<!--      <el-table-column prop="note" label="已使用"></el-table-column>-->
      <el-table-column align="center" prop="status" label="状态"  width="100">
        <template #default="{ row }">
          <el-text v-if="row.status === 0" type="success">正常</el-text>
          <el-text v-else-if="row.status === 1" type="info">停用</el-text>
          <el-text v-else type="info"></el-text>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="180" fixed="right">
        <template #default="{ row }">
          <el-button link type="primary" @click="handleEdit(row,{sid:row.sid})">编辑</el-button>
          <el-popconfirm :persistent="false" title="确定删除吗？" confirm-button-text="确定" cancel-button-text="取消"
                         @confirm="handleAction('delete',`/coupons/batchDel`,{sidList:[row.sid]})">
            <template #reference>
              <el-button link type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
        background
        v-model:current-page="page"
        v-model:page-size="pageSize"
        :page-sizes="pageSizeNum"
        :total="totalNums"
        layout="total, sizes, prev, pager, next, jumper"
    >
    </el-pagination>

    <el-dialog v-model="dialogShow" :title="dialogParams.type == 'add'?'添加':'编辑'" width="660px" :append-to-body="true" :destroy-on-close="true" :close-on-click-modal="false" @close="closeDialog">
      <el-form ref="dialogFormRef" :model="dialogForm" :rules="dialogFormRules" label-width="120px" style="width: 560px">
        <el-form-item label="优惠卷名称" prop="couponName" required>
          <el-input v-model.trim="dialogForm.couponName" clearable placeholder="请输入优惠卷名称"/>
        </el-form-item>
        <el-form-item label="优惠卷描述" prop="description">
          <el-input v-model.trim="dialogForm.description" clearable placeholder="请输入优惠卷描述"  :rows="2" type="textarea"/>
        </el-form-item>
<!--        <el-form-item label="最低消费金额" prop="minSpend">-->
<!--          <el-input-number v-model.trim="dialogForm.minSpend" :min="0" controls-position="right"  :precision="2" :step="0.1">-->
<!--            <template #prefix>-->
<!--              <span>￥</span>-->
<!--            </template>-->
<!--          </el-input-number>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="满减金额" prop="amountOff">-->
<!--          <el-input-number v-model="dialogForm.amountOff" :min="0" controls-position="right"  :precision="2" :step="0.1">-->
<!--            <template #prefix>-->
<!--              <span>￥</span>-->
<!--            </template>-->
<!--          </el-input-number>-->
<!--        </el-form-item>-->
        <el-form-item label="开始时间" prop="startDate">
          <el-date-picker
              v-model="dialogForm.startDate"
              type="datetime"
              placeholder="请选择时间"
              :shortcuts="datePickerShortcuts"
              format="YYYY-MM-DD HH:mm:ss"
              value-format="YYYY-MM-DD HH:mm:ss"
          />
        </el-form-item>
        <el-form-item label="结束时间" prop="endDate">
          <el-date-picker
              v-model="dialogForm.endDate"
              type="datetime"
              placeholder="请选择时间"
              :shortcuts="datePickerShortcuts"
              format="YYYY-MM-DD HH:mm:ss"
              value-format="YYYY-MM-DD HH:mm:ss"
          />
        </el-form-item>
        <el-form-item label="优惠卷状态" prop="couponStatus">
          <el-radio-group v-model="dialogForm.couponStatus">
            <el-radio :value="0">正常</el-radio>
            <el-radio :value="1">停用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="dialogForm.status">
            <el-radio :value="0">正常</el-radio>
            <el-radio :value="1">停用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item>
          <div class="flexs">
            <el-button type="primary" :loading="dialogParams.loading" @click="submitDialogForm(dialogFormRef)">
              确认
            </el-button>
            <el-button @click="resetDialogForm(dialogFormRef)">重置</el-button>
          </div>
        </el-form-item>
      </el-form>
    </el-dialog>

    <go-back/>
  </div>
</template>

<script setup>
import {
  getCurrentInstance,
  ref,
  reactive,
  onMounted,
  watch,
  computed
} from "vue"
import listMixins from "@mixins/hook/list"
import {
  useRouter
} from "vue-router"
import {
  CirclePlus, Search
} from "@element-plus/icons-vue"
import GoBack from "@components/GoBack.vue"
const router = useRouter()
const {
  proxy
} = getCurrentInstance()

/**
 * minxin列表加载
 */
const {
  page,
  pageSizeNum,
  pageSize,
  loading,
  totalNums,
  searchParams,
  searchBtnLoading,
  apiPath,
  list,
  dialogFormRef,
  dialogShow,
  dialogForm,
  dialogParams,
  handleSearch,
  getData,
  handleAdd,
  handleEdit,
  resetDialogForm,
  handleAllSelected,
  submitDialogForm,
  closeDialog,
  handleAction,
  handleDelete
} = listMixins()

dialogForm.amountOff = "";
dialogForm.couponName	= "";
dialogForm.couponStatus	= 0;
dialogForm.description	= "";
dialogForm.endDate	= "";
dialogForm.minSpend	= "";
dialogForm.startDate = "";
dialogForm.status	= 0;

apiPath.value = '/coupons';

onMounted(() => {
  getData()
})
watch(()=>dialogForm.startDate,newValue => {
  dialogFormRef.value.validateField(`endDate`);
});
const dialogFormRules = reactive({
  couponName:[{
    required: true,
    message: "请输入优惠卷名称",
    trigger: "blur"
  }],
  startDate:[{
    required: true,
    message: "请输入选择开始时间",
    trigger: "change"
  }],
  endDate:[
    {
      required: true,
      message: "请输入选择结束时间",
      trigger: "change"
    },
    {
      validator:(rule,value,callback)=>{
        if(!dialogForm.startDate){
          callback()
        }else{
          let startTime = new Date(dialogForm.startDate).getTime();
          let endTime = new Date(value).getTime();
          if(startTime<endTime){
            callback()
          }else{
            callback(new Error('结束时间需晚于开始时间。'))
          }
        }
      },
      rigger: 'change',
    }
  ]
})
const datePickerShortcuts = [
  {
    text: '今天',
    value: new Date(),
  },
  {
    text: '昨天',
    value: () => {
      const date = new Date()
      date.setDate(date.getDate() - 1)
      return date
    },
  },
  {
    text: '一周前',
    value: () => {
      const date = new Date()
      date.setDate(date.getDate() - 7)
      return date
    },
  },
]
</script>

<style lang="less">

</style>